package WebServices
{
	import DataModel.ReferenceData;
	import DataModel.SkillsAuditDocument;
	
	import flash.net.URLVariables;
	
	internal class LoginResponder extends HttpResponder
	{
		public function LoginResponder(auth:AuthenticationData, success:Function, failure:Function)
		{
			super(auth, success, failure);
		}

		public function callServer(service: SkillsAuditService, userName:String, password:String):void
		{
			var xml:XML =
			<ReferenceDataAccessRequest>
				<SecurityContext>
  					<UserName/> 
  					<UserPasswordMD5Hash/> 
  				</SecurityContext>
  			</ReferenceDataAccessRequest>

			m_authData.username = userName;
			m_authData.password = password;  			
  			xml.SecurityContext.UserName = userName;
  			xml.SecurityContext.UserPasswordMD5Hash = m_authData.getDigest();
			service.setCallbacks(onSuccess, onFailure);
			var strXML:String = "xml="+xml.toXMLString();
			var myPattern:RegExp = /&/g;  
			strXML = strXML.replace(myPattern, "%26"); 
			service.request= new URLVariables(strXML);
			service.send();
		}

		override protected function onSuccess(xmlResponse:XML):void
		{
			if(xmlResponse.name().toString() != "ReferenceDataAccessResponse")
			{
				onFailure(new Error("Invalid root element received from server. Expected 'EnumDataAccessResponse' received '" +xmlResponse.name().toString() + "'"));
				return;
			}
			ReferenceData.instance.loadAcadInstitutions(xmlResponse.AcademicInstitution.AcademicInstitutionData);
			ReferenceData.loadMinistries(xmlResponse.Ministries);
			ReferenceData.loadDepartments(xmlResponse.Departments);
			ReferenceData.loadCaseTypes(xmlResponse.CaseType);
			ReferenceData.loadProvinces(xmlResponse.Provinces);
			ReferenceData.loadDistricts(xmlResponse.Districts);
			ReferenceData.loadPayStations(xmlResponse.PayStations);
			ReferenceData.loadCountries(xmlResponse.Countries);
			ReferenceData.loadInstitutionsTypes(xmlResponse.InstitutionsTypes);
			ReferenceData.loadAcadFields(xmlResponse.AcadFields);
			ReferenceData.loadQualifications(xmlResponse.Qualifications);
			SkillsAuditDocument.instance.accessLevel = xmlResponse.UserAccessLevel;
			m_fnSuccess([UserRole.DataEntryOperator, UserRole.DataCheckOperator]);
		}
		
//Test data - should no longer be needed --PB
//			var fakeResponse:XML = 			
//				<ReferenceDataAccessResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
//				   xsi:noNamespaceSchemaLocation="file:///C:/DOCUME~1/Philip/LOCALS~1/Temp/ReferenceDataAccessResponse-2.xsd">
//				  <DataSources>
//				    <DataSource>
//				      <DataSourceID>9761</DataSourceID>
//				      <DataSourceDesc>string</DataSourceDesc>
//				    </DataSource>
//				    <DataSource>
//				      <DataSourceID>5465</DataSourceID>
//				      <DataSourceDesc>string</DataSourceDesc>
//				    </DataSource>
//				    <DataSource>
//				      <DataSourceID>5266</DataSourceID>
//				      <DataSourceDesc>string</DataSourceDesc>
//				    </DataSource>
//				  </DataSources>
//				  <Departments>
//				    <Department>
//				      <DepartmentCode>0000</DepartmentCode>
//				      <DepartmentDesc>dept aaaa</DepartmentDesc>
//				    </Department>
//				    <Department>
//				      <DepartmentCode>1111</DepartmentCode>
//				      <DepartmentDesc>dept bbbb</DepartmentDesc>
//				    </Department>
//				    <Department>
//				      <DepartmentCode>2222</DepartmentCode>
//				      <DepartmentDesc>dept cccc</DepartmentDesc>
//				    </Department>
//				  </Departments>
//				  <Districts>
//				    <District>
//				      <DistrictCode>21</DistrictCode>
//				      <DistrictName>District A1</DistrictName>
//				    </District>
//				    <District>
//				      <DistrictCode>22</DistrictCode>
//				      <DistrictName>District A2</DistrictName>
//				    </District>
//				    <District>
//				      <DistrictCode>23</DistrictCode>
//				      <DistrictName>District A3</DistrictName>
//				    </District>
//				  </Districts>
//				  <Ministries>
//				    <Ministry>
//				      <MinistryID>1</MinistryID>
//				      <MinistryDesc>string1 &amp; entity test</MinistryDesc>
//				    </Ministry>
//				    <Ministry>
//				      <MinistryID>5</MinistryID>
//				      <MinistryDesc>string5</MinistryDesc>
//				    </Ministry>
//				    <Ministry>
//				      <MinistryID>99</MinistryID>
//				      <MinistryDesc>string99</MinistryDesc>
//				    </Ministry>
//				  </Ministries>
//				  <MinistryCodeMaps>
//				    <MinistryCodeMap>
//				      <ID>5684</ID>
//				      <MinistryID>2399</MinistryID>
//				      <MinistryDesc>string</MinistryDesc>
//				      <SSBMinistryID>9406</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </MinistryCodeMap>
//				    <MinistryCodeMap>
//				      <ID>5639</ID>
//				      <MinistryID>5415</MinistryID>
//				      <MinistryDesc>string</MinistryDesc>
//				      <SSBMinistryID>2987</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </MinistryCodeMap>
//				    <MinistryCodeMap>
//				      <ID>5054</ID>
//				      <MinistryID>2612</MinistryID>
//				      <MinistryDesc>string</MinistryDesc>
//				      <SSBMinistryID>9863</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </MinistryCodeMap>
//				  </MinistryCodeMaps>
//				  <PayStations>
//				    <PayStation>
//				      <MinistryID>1</MinistryID>
//				      <DeptCode>0000</DeptCode>
//				      <PayStationCode>02</PayStationCode>
//				      <PayStationDesc>Pay station 02</PayStationDesc>
//				      <RegionCode>string</RegionCode>
//				      <ProvinceCode>string</ProvinceCode>
//				      <DistrictCode>string</DistrictCode>
//				    </PayStation>
//				    <PayStation>
//				      <MinistryID>1</MinistryID>
//				      <DeptCode>0000</DeptCode>
//				      <PayStationCode>01</PayStationCode>
//				      <PayStationDesc>Pay station 01</PayStationDesc>
//				      <RegionCode>string</RegionCode>
//				      <ProvinceCode>string</ProvinceCode>
//				      <DistrictCode>string</DistrictCode>
//				    </PayStation>
//				    <PayStation>
//				      <MinistryID>1</MinistryID>
//				      <DeptCode>0000</DeptCode>
//				      <PayStationCode>03</PayStationCode>
//				      <PayStationDesc>Pay station 03</PayStationDesc>
//				      <RegionCode>string</RegionCode>
//				      <ProvinceCode>string</ProvinceCode>
//				      <DistrictCode>string</DistrictCode>
//				    </PayStation>
//				  </PayStations>
//				  <Provinces>
//				    <Province>
//				      <ProvinceCode>11</ProvinceCode>
//				      <ProvinceName>Province P1</ProvinceName>
//				    </Province>
//				    <Province>
//				      <ProvinceCode>12</ProvinceCode>
//				      <ProvinceName>Province P2</ProvinceName>
//				    </Province>
//				    <Province>
//				      <ProvinceCode>13</ProvinceCode>
//				      <ProvinceName>Province P3</ProvinceName>
//				    </Province>
//				  </Provinces>
//				  <Regions>
//				    <Region>
//				      <RegionCode>string</RegionCode>
//				      <RegionName>string</RegionName>
//				    </Region>
//				    <Region>
//				      <RegionCode>string</RegionCode>
//				      <RegionName>string</RegionName>
//				    </Region>
//				    <Region>
//				      <RegionCode>string</RegionCode>
//				      <RegionName>string</RegionName>
//				    </Region>
//				  </Regions>
//				  <SSBMinistryCodes>
//				    <SSBMinistryCode>
//				      <ID>2795</ID>
//				      <SSBMinistryID>138</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </SSBMinistryCode>
//				    <SSBMinistryCode>
//				      <ID>9195</ID>
//				      <SSBMinistryID>3447</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </SSBMinistryCode>
//				    <SSBMinistryCode>
//				      <ID>8234</ID>
//				      <SSBMinistryID>5769</SSBMinistryID>
//				      <SSBMinistryDesc>string</SSBMinistryDesc>
//				      <SSMA_TimeStamp>1697-02-01T00:00:00Z</SSMA_TimeStamp>
//				    </SSBMinistryCode>
//				  </SSBMinistryCodes>
//				  <TOECode>
//				    <TOECode>
//				      <TOEID>3806</TOEID>
//				      <TOEDesc>string</TOEDesc>
//				    </TOECode>
//				    <TOECode>
//				      <TOEID>1</TOEID>
//				      <TOEDesc>string</TOEDesc>
//				    </TOECode>
//				    <TOECode>
//				      <TOEID>4698</TOEID>
//				      <TOEDesc>string</TOEDesc>
//				    </TOECode>
//				  </TOECode>
//				</ReferenceDataAccessResponse>;
//			//xmlResponse = fakeResponse;
		
	}
}